//
//  BBCIImageBlobDetector.m
//  BBTouch
//
//  Created by ben smith on 6/14/08.
//  This file is part of BBTouch.
//
//  BBTouch is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  BBTouch is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.

//  You should have received a copy of the GNU Lesser General Public License
//  along with BBTouch.  If not, see <http://www.gnu.org/licenses/>.
// 
//  Copyright 2008 Ben Britten Smith ben@benbritten.com .
//


#import "BBCIImageBlobDetector.h"


@implementation BBCIImageBlobDetector
// TODO: finish this object

-(void)analyze:(CIImage*)anImage
{
	// STUB, unfinished
	/*
	if (bgImage == nil) return; // cant do anything without a background image

	int width = [ciImage extent].size.width;
	int rows = [ciImage extent].size.height;
	int rowBytes = (width * 4);
	
	NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil 
																																	pixelsWide:width 
																																	pixelsHigh:rows 
																															 bitsPerSample:8 
																														 samplesPerPixel:4 
																																		hasAlpha:YES 
																																		isPlanar:NO 
																															colorSpaceName:NSCalibratedRGBColorSpace 
																																bitmapFormat:0 
																																 bytesPerRow:rowBytes 
																																bitsPerPixel:0];
	
	CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName ( kCGColorSpaceGenericRGB );
	CGContextRef context = CGBitmapContextCreate( [rep bitmapData], width, rows, 8, rowBytes, colorSpace, kCGImageAlphaPremultipliedLast );
	
	CIContext* ciContext = [CIContext contextWithCGContext:context options:nil];
	[ciContext drawImage:ciImage atPoint:CGPointZero fromRect: [ciImage extent]];
	
	CGContextRelease( context );
	CGColorSpaceRelease( colorSpace );
		
	NSBitmapImageRep * bitRep = [[anImage representations] objectAtIndex:0];
	[BBImagePreProcessor borderROI:bitRep roi:roi];
	srcBytes = [bitRep bitmapData];
	
	NSBitmapImageRep * bgBitRep = [[bgImage representations] objectAtIndex:0];
	bgBytes = [bgBitRep bitmapData];
	
	[self analyzeRawData];
*/
}
	 
///////////////////////////
// BG image methods
///////////////////////////
-(CIImage*)bgImage
{
	return cibgImage;
}

-(void)setBgImage:(CIImage*)anImage
{
	//STUB, unfinished
	/*
	// sets the background image that gets subtracted from the foreground
	// i can use the first object in the reps because I know the cam is only adding a 
	// single rep. Altho this does work in the general case, usually
	NSBitmapImageRep * bitRep = [[anImage representations] objectAtIndex:0]; 
	
	// set up all the state vars here
	samplesPerPixel = [bitRep samplesPerPixel];
	bytesPerRow = [bitRep bytesPerRow];
	pixelsHigh = [bitRep pixelsHigh];
	pixelsWide = [bitRep pixelsWide];
	srcBytes = [bitRep bitmapData];
	maxDataPosition = pixelsHigh * bytesPerRow;
	
	// this should only need to be done once for any given image size	
	// but for now we do it every time we set the BG for simplicity sake
	[self setupNeighborGrid];
	
	[self setupStateArrays];
	// bitmapreps were never meant to be stored, so we will store the image instead
	[anImage retain];
	[bgImage release];
	bgImage = anImage;
	 */
}

@end
